package cn.xzq.crm.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import cn.xzq.crm.entity.User;
import cn.xzq.crm.service.UserService;

@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserService userService;
	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * 登录
	 */
	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		//验证码验证
		String tcode = request.getParameter("txtcode").toLowerCase();
		String vcode = ((String) session.getAttribute("vCode")).toLowerCase();
		if(!tcode.equals(vcode)){
			request.setAttribute("form", user);
			request.setAttribute("msg", "验证码错误！");
			return "login";
		}
		try{
			User userEx = userService.login(user);
			if(userEx != null){
				session.setAttribute("session_user", userEx);
				return "loginsuccess";
			}else{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
				request.setAttribute("msg", user.getUsername()+"用户没有注册!");
				request.setAttribute("form", user);
				return "login";
			}
		}catch (Exception e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("form", user);
			return "login";
		}
	}
	
}
